/**
 * deleteFollow node file
 */

var deleteFollow = function(accountId, followerId, options){
	this.accountId = accountId;
	this.followerId = followerId;
	this.success = options.success;
	this.error = options.error;
	this.Delete();
};

deleteFollow.prototype._success = function(data){
	if(typeof this.success == 'function') {
		this.success(data);
	}
};

deleteFollow.prototype._error = function(data){
	if(typeof this.error == 'function') {
		this.error(data);
	}
};

deleteFollow.prototype.check = function(nextStatement){
	var self = this;
	Mysql.query("SELECT COUNT(*) AS c1 FROM Follow WHERE accountId="+Mysql.escape(self.accountId)+" AND followerId="+Mysql.escape(self.followerId)+" LIMIT 1", function(error, results){
		if(error) {
			console.log('Mysql error: '+error);
			self._error(9999, 'Mysql error');
			return;
		}
		if(results[0].c1 == 0) {
			self._error(9999, 'Not found following');
			return;
		}
		nextStatement();
	});
};

deleteFollow.prototype.Delete = function(){
	var self = this;
	self.check(function(){
		Mysql.query("DELETE FROM Follow WHERE accountId="+Mysql.escape(self.accountId)+" AND followerId="+Mysql.escape(self.followerId)+" LIMIT 1", function(error, results){
			if(error) {
				console.log('Mysql error: '+error);
				self._error(9999, 'Mysql error');
				return;
			}
			self._success({ accountId: self.accountId, followerId: self.followerId });
			return;
		});
	});
};

module.exports = deleteFollow;